<?php

namespace app\cash\controller;

use app\BaseController;
use app\common\logic\TokenLogic;
use app\common\model\account\AccountModel;
use app\common\model\account\ShopAccountModel;

class Login extends BaseController
{

    public function login()

    {

        $shop_id = $this->request->param('shop_id/d', 0);

        $mobile = $this->request->param('mobile');

        $shop_password = $this->request->param('shop_password');

        if ($shop_id < 1) {

            return $this->error('门店ID号不正确');

        }

        if (empty($mobile)) {

            return $this->error('请输入手机号');

        }

        if (empty($shop_password)) {

            return $this->error('请输入密码');

        }

        $where = [

            ['mobile', '=', $mobile],

            ['shop_password', '=', md5($shop_password)]

        ];
        $account = AccountModel::with(['shopAccount' => function ($query) use ($shop_id) {

            $query->where(['role_id' => 3, 'shop_id' => $shop_id]);

        }])->where($where)->find();

        if (null === $account) {

            return $this->error('手机号或者密码不正确');

        }

        if ($account->is_delete != 0) return $this->error('手机号或者密码不正确');

        if ($account->status != 1) return $this->error('您已经离职了');

        if (empty($account->shopAccount)) return $this->error('该账号无权限');

        $auth_token = TokenLogic::getShopToken($account->shopAccount->shop_id, $account->account_id, 3);

        return $this->success(['auth_token' => $auth_token]);

    }

    public function socketbind()
    {
        $accountId = $this->request->accountId;
        $shop_id = $this->request->shopId;
        $uid = $this->request->param('uid');
        if (empty($uid)) {
            return $this->error('uid参数不能为空');
        }
        $account = ShopAccountModel::where([
            ['account_id', '=', $accountId],
            ['shop_id', '=', $shop_id]])->find();
        if (empty($account)) {
            return $this->error('账号不存在');
        }
        $up['uid'] = $uid;
        ShopAccountModel::where([
            ['account_id', '=', $accountId],
            ['shop_id', '=', $shop_id]])->update($up);
        return $this->success();
    }

    public function home()

    {

        $shop = $this->request->shop;

        $account = $this->request->account;

        $datas = [

            'shop_name' => $shop->name,

            'account_name' => $account->username,

            'account_face' => $account->face,

        ];

        return $this->success($datas);

    }
}

